-- SkyBloodyCave
-- Created by huanghr July/26/2016
-- 血腥地窖

-- 初始化
function initSkyBloodyCave(self)
    self.item:removeAllChildren();

    local unrealId = self.gridData.unreal_id;

    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);
    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end
    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            -- 获取格子位置
            local pos = self.gridData:getPos();

            -- 音效
            AudioM.playFx("ui_open1");


            if not SkyInstanceM.guardStatueAllClear() then
                -- 所有守卫雕像清掉才能打开
                self:openBuildingView(nil, -20, buildingInfo["dialog2"], getLocStr("btn_text_leave"), nil, iconPath);
                return;
            end

            local function okFunc()
                -- 进入前提示
                local beforeEnterTip = UnrealDungeonM.query(unrealId, "before_enter_tip");
                if type(beforeEnterTip) == "string" and beforeEnterTip ~= "" then
                    alert(beforeEnterTip);
                end

                -- 进入夹层
                DungeonActionM.go("start_unreal", pos, unrealId);
            end

            self:openBuildingView(okFunc, nil, nil, nil, nil, iconPath);
        end
    end

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);

    -- 发光
    if self.gridData.need_shining == 1 then
        shiningEffect(self.icon, 70, 100);
    end
end
